package com.sduept.nwld.dataserver.controller.daily;

import com.sduept.nwld.dataserver.manager.protection.OperationRiskManager;
import com.sduept.nwld.dataserver.model.daily.OperationaRisk;
import com.sduept.utils.DateUtil;
import org.primefaces.event.CellEditEvent;
import org.primefaces.event.SelectEvent;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@Named
@ViewScoped
public class OperationRiskController extends DailyReportAbstractController {
	/**
	 * 电网保护运行风险controller类
	 */

	// 通过判断实体类的serialVersionUID来验证版本一致性的
	private static final long serialVersionUID = 797974837070036813L;

	@Autowired
	private OperationRiskManager po;

	private List<OperationaRisk> risk = new ArrayList<>();
	private OperationaRisk selectRisk = null;
	private OperationaRisk created = new OperationaRisk();

	@PostConstruct
	public void init() {
		String start = DateUtil.getStringDate(queryDate1, DateUtil.pattern_ymdhms);
		String end = DateUtil.getStringDate(queryDate2, DateUtil.pattern_ymdhms);
		risk = po.getOperationaRisk(start, end);
	}

	public void refreshPage() {
		initParamter();
		init();
	}

	public void onRowSelect(SelectEvent event) {
		selectRisk = (OperationaRisk) event.getObject();
	}

	public void update() {
		if (selectRisk != null) {
			po.editUpdate(selectRisk);
			addSuccessMessage("保存成功");
		}
	}

	/**
	 * 新建按钮触发事件
	 */
	public void createRisk() {
		created = new OperationaRisk();
	}

	/**
	 * opendialog保存按钮触发事件
	 */
	public void saveNew() {
		if (created != null) {
			created = po.creatOperationaRisk(created);
			risk.add(0, created);
		}
		created = null;
		addSuccessMessage("保存成功");
	}

	public void deleteRisk() {
		if (selectRisk != null) {
			if (selectRisk != null) {
				po.delOperationaRisk(selectRisk);
				risk.remove(selectRisk);
				selectRisk = null;
			}
		} else {
			addErrorMessage("请选择一条数据进行删除");
		}
	}

	public void onRiskRowSelect(SelectEvent event) {
		selectRisk = (OperationaRisk) event.getObject();
	}

	public void onRiskCellEdit(CellEditEvent event) {
		selectRisk = null;
		for (OperationaRisk k : risk) {
			if (k.getId().equals(event.getRowKey())) {
				selectRisk = k;
				break;
			}
		}
		if (selectRisk != null) {
			po.updateOperationaRisk(selectRisk);
		}
	}

	public void resetQuery() {
		queryDate1 = null;
		queryDate2 = null;
		resetqueryDate();
		queryByTime();
		selectRisk = null;
		created = null;
	}

	public void queryByTime() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String start;
		String end;
		if (queryDate1 == null && queryDate2 == null) {
			risk = po.getOperationaRisk();
		} else {
			start = "1970-01-01 00:00:00";
			end = format.format(Calendar.getInstance().getTime());
			if (queryDate1 != null) {
				start = format.format(queryDate1);
			}
			if (queryDate2 != null) {
				end = format.format(queryDate2);
			}
			risk = po.getOperationaRisk(start, end);
		}
		refreshQueryParamter();
		selectRisk = null;
	}

	public List<OperationaRisk> getRisk() {
		return risk;
	}

	public void setRisk(List<OperationaRisk> risk) {
		this.risk = risk;
	}

	public OperationaRisk getSelectRisk() {
		return selectRisk;
	}

	public void setSelectRisk(OperationaRisk selectRisk) {
		this.selectRisk = selectRisk;
	}

	public OperationaRisk getCreated() {
		return created;
	}

	public void setCreated(OperationaRisk created) {
		this.created = created;
	}

}
